// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004-2009 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace SoapTrace
{
	/// <summary>
	/// A form that allows the user to define text searches.
	/// </summary>
	public partial class FindTextForm : Form
	{
		/// <summary>
		/// The find next event delegate.
		/// </summary>
		public delegate void FindNextEventHandler( object sender, 
			FindNextEventArgs e );

		/// <summary>
		/// The find next event triggered by the user clicking the 
		/// Find next button.
		/// </summary>
		private event FindNextEventHandler FindNextEvent;

		/// <summary>
		/// Creates a new instance of the <see cref="FindTextForm"/> class.
		/// </summary>
		public FindTextForm( )
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent( );
		}

		/// <summary>
		/// Event that is triggered when the user clicks the Find Next button.
		/// </summary>
		public event FindNextEventHandler FindNext
		{
			add
			{
				FindNextEvent += value;
			}

			remove
			{
				FindNextEvent -= value;
			}
		}

		/// <summary>
		/// Gets or sets whether the search should be case sensitive.
		/// </summary>
		public bool CaseSensitive
		{
			[ DebuggerStepThrough ]
			get
			{
				return MatchCaseCheckBox.Checked;
			}

			[ DebuggerStepThrough ]
			set
			{
				MatchCaseCheckBox.Checked = value;
			}
		}

		/// <summary>
		/// Gets or sets the text to find.
		/// </summary>
		public string FindText
		{
			[ DebuggerStepThrough ]
			get
			{
				return TextToFindTextBox.Text;
			}

			set
			{
				TextToFindTextBox.Text = value;

				if( value.Length == 0 )
				{
					FindNextButton.Enabled = false;
				}

				TextToFindTextBox.Focus( );
			}
		}

		/// <summary>
		/// Find next event received.
		/// </summary>
		protected virtual void OnFindNext( )
		{
			if( FindNextEvent != null ) 
			{
				FindNextEventArgs Args = new FindNextEventArgs( TextToFindTextBox.Text, MatchCaseCheckBox.Checked );
				FindNextEvent( this, Args ); 
			}
		}

		/// <summary>
		/// Handles the cancel button click event.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void CancelButtonClick( object sender, EventArgs e )
		{
			Close( );
		}

		/// <summary>
		/// Handles the text box TextChanged event.  This disables
		/// the Find Next button if there is no text in the text box.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void TextToFindFindTextChanged( object sender, System.EventArgs e )
		{
			if( TextToFindTextBox.Text.Length == 0 )
			{
				FindNextButton.Enabled = false;
			}
			else
			{
				FindNextButton.Enabled = true;
			}
		}

		/// <summary>
		/// The user clicked the find next button.
		/// </summary>
		/// <param name="sender">The event source.</param>
		/// <param name="e">The event arguments.</param>
		private void FindNextButtonClick( object sender, EventArgs e )
		{
			OnFindNext( );
		}
	}
}
